package com.fzrskj.cloud.free.sys.utils;

import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.sys.vo.AgentAccountMessageVo;
import com.fzrskj.cloud.free.user.bean.UserAccountView;
import com.fzrskj.cloud.free.user.utils.UserAccountUtils;
import ms.core.tool.EncryptTool;
import ms.core.tool.StrTool;

import javax.servlet.http.HttpServletRequest;

/**
 * AgentRequestUtils.
 *
 * @author zj.
 *         Created on 2019/5/10 0010.
 */
public class AgentAccountUtils {
    /**
     * 设置代理商信息.
     * @param request HttpServletRequest.
     * @return set result.
     */
    public static Boolean setAgentInfo(HttpServletRequest request, AgentAccountMessageVo messageVo) {
        String agentInformation = request.getHeader("agent-information");
        if (StrTool.isBlankStr(agentInformation)) {
            CloudContextUtils.setAgentId(CloudContextUtils.DEF_AGENT_ID);
        } else {
            messageVo.setCode("UNRECOGNIZABLE_AUTHENTICATION_INFORMATION");
            messageVo.setMessage("不可识别的鉴权信息");
            String[] agentItem = agentInformation.split(";");
            if (agentItem.length != 2 && agentItem.length != 3) {
                return false;
            }
            String userName = "";
            String apiPassword = "";
            String nonce = "";
            for (String itemString : agentItem) {
                String [] item = itemString.split("=");
                if (item.length != 2) {
                    return false;
                }
                if ("id".equals(item[0].trim())) {
                    userName = item[1].trim();
                }

                if ("sign".equals(item[0].trim())) {
                    apiPassword = item[1].trim();
                }

                if ("nonce".equals(item[0].trim())) {
                    nonce = item[1].trim();
                }
            }

            if (StrTool.isBlankStr(userName) || StrTool.isBlankStr(apiPassword)) {
                return false;
            }
            if (agentItem.length == 3 && StrTool.isBlankStr(nonce)) {
                return false;
            }


            //获取账号.
            UserAccountUtils userAccountUtils = new UserAccountUtils();
            UserAccountView account = userAccountUtils.queryAgentByAccessKey(userName);
            if (null == account) {
                messageVo.setCode("UNRECOGNIZABLE_AGENT_INFORMATION");
                messageVo.setMessage("不可识别的代理信息");
                return false;
            }

            //是否为代理商.
            if (!"Y".equals(account.getIsAgent())) {
                messageVo.setCode("NO_PRODUCT_AGENCY_AUTHORITY");
                messageVo.setMessage("当前账号无代理权限");
                return false;
            }

            //状态是否正常.
            if (!"Y".equals(account.getStatus())) {
                messageVo.setCode("ACCOUNT_UNAVAILABLE");
                messageVo.setMessage("当前账号已被冻结，不可用");
                return false;
            }

            //检查api密钥.
            if (agentItem.length == 3) {
                String checkPass = EncryptTool.md5Encode(userName + "\n"+ account.getAgentApiPass() + "\n" + nonce);
                if (!checkPass.toLowerCase().equals(apiPassword.toLowerCase())) {
                    messageVo.setCode("INTERFACE_AUTHENTICATION_FAILED");
                    messageVo.setMessage("接口认证失败");
                    return false;
                }
                messageVo.setNewSession(true);
                messageVo.setUserAccount(account);
                CloudContextUtils.setAgentId(account.getAgentId());
            } else {
                String checkPass = EncryptTool.md5Encode(userName + EncryptTool.md5Encode(account.getAgentApiPass()));
                if (!checkPass.toLowerCase().equals(apiPassword.toLowerCase())) {
                    messageVo.setCode("INTERFACE_AUTHENTICATION_FAILED");
                    messageVo.setMessage("接口认证失败");
                    return false;
                }
                CloudContextUtils.setAgentId(account.getUserId());
            }
        }
        return true;
    }
}
